package com.springcloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.springcloud.model.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ProviderController {
    @RequestMapping("/test")
    public Object test(){
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "服务提供者8082";
    }

    @RequestMapping("/getUser")
    public Object getUser(){
        return new User("张三",23);
    }

    @RequestMapping("/getUserList")
    public Object getUserList(){
        List list=new ArrayList<User>();
        list.add(new User("张三",23));
        list.add(new User("李四",24));
        return list;
    }

    @RequestMapping("/params01")
    public Object params01(String name,Integer age){
        return new User(name+"--服务提供者",age);
    }
    @RequestMapping("/paramsObj")
    public Object paramsObj(@RequestBody User user){
        return new User(user.getName()+"--服务提供者",user.getAge());
    }
    @RequestMapping("/paramsList")
    public Object paramsList(@RequestBody List<User> list){
        list.forEach(obj->obj.setName(obj.getName()+"---服务提供者"));
        return list;
    }
    @RequestMapping("/paramsJson")
    public Object paramsList(String paramsJson){
        User user= JSONObject.parseObject(paramsJson,User.class);
        user.setName(user.getName()+"---服务提供者");
        return user;
    }
}
